/*
 * Copyright (c) 2007 centrevillage <centrevillage@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

// std
#include <string>

// boost
#include <boost/format.hpp>

// seq
#include "MidiManager.h"
#include "Sequence.h"
#include "MidiException.h"

// ravi
#include "types.hpp"
#include "error.hpp"
#include "evaluates.hpp"
#include "options.hpp"
#include "function_util.hpp"
#include "compiler.hpp"

#include "ConfigLoader.h"

// define macro
#define SEQ_BEGIN try
#define SEQ_END   catch (SeqException& e) {throw script_error(io::str(format("Seq Error: [%1%]")%e.what()));}

using namespace seq;
using namespace boost;
using namespace std;

namespace ravi {

val_t option_division(arg_t& argv) {
    argv_guard_type(argv, 0, int_id);
    SEQ_BEGIN {
        int division = get_int(argv[0]);
        if (!(120 <= division && division <= 960)) throw compile_error("option \"division\" must be in range 120..960.");
        SequencerCore& core = SequencerCore::instance();
        Project& proj = core.getProject();
        proj.setTickPer4((unsigned) division);
    } SEQ_END
    return make_val();
}

val_t option_loadpath(arg_t& argv) {
    argv_guard_type(argv, 0, map_id);
    SEQ_BEGIN {
        map_t* mv = get_map(argv[0]);
        map_t::iterator i = mv->begin(), e = mv->end();
        for (;i!=e;++i) {
            if (       !is_str(i->first)
                    || !is_valid_list_format(i->second, "s+", true)
                    || !g_config_loader.isValidCategory(get_string(i->first))) {
                throw compile_error("'loadpath' option value format is invalid.");
            }
        }
        g_config_loader.clearLoadPath();
        i = mv->begin();
        for (;i!=e;++i) {
            lst_t* lv = get_lst(i->second);
            lst_t::iterator j = lv->begin(), je = lv->end();
            for (;j!=je;++j) {
                g_config_loader.addLoadPath(get_string(i->first), eval_string(*j));
            }
        }
    } SEQ_END
    return make_val();
}

val_t option_undo(arg_t& argv) {
    argv_guard_type(argv, 0, int_id);
    SEQ_BEGIN {
        SequencerCore& core = SequencerCore::instance();
        if (get_int(argv[0]) != 0) {
            core.setUseUndo(true);
        } else {
            core.setUseUndo(false);
        }
    } SEQ_END
    return make_val();
}

} // end of namespace ravi
